//
//  SendWifiDataTool.m
//  Splash
//
//  Created by Mac on 16/4/1.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "SendWifiDataTool.h"


@interface   SendWifiDataTool()
@property (nonatomic, strong) AsyncUdpSocket *socket;
@end

#define protocolVersion  1  //协议版本


@implementation SendWifiDataTool
SingletonM(SendWifiDataTool)

- (AsyncUdpSocket *)socket
{
    if (_socket == nil) {
        _socket = [[AsyncUdpSocket alloc] initIPv4];
        [_socket setDelegate:self];//这步很重要，否则无法自定义监听方法
    }
    return _socket;
}


- (void)sendWifiMsg:(NSString*)sendStr
{
//    [self socket];
    
    NSData *data =  [sendStr dataUsingEncoding:NSUTF8StringEncoding];
    [self.socket sendData: data toHost:WifiIP port:socketNumber withTimeout:-1 tag:0];
    
    //启动接收线程
    [self.socket receiveWithTimeout:-1 tag:0];
}


//发送单个颜色的命令
- (void)sendSingleRGBColorCmdRedValue:(NSString*)redValue blueValue:(NSString*)blueValue  greenValue:(NSString*)greenValue
{
    unsigned char cmdData[] = {0xAA,0x0D,0x01,0x00,0x00,0x01,0x01,0x10,0x02};
    unsigned char checkData  = 0;
    unsigned char redData = [redValue intValue];
    unsigned char blueData = [blueValue intValue];
    unsigned char greenata = [greenValue intValue];
    for (int i = 0; i<9; i++) {
        checkData += cmdData[i];
    }
    checkData += redData;
    checkData += blueData;
    checkData += greenata;
    
    NSString *sendStr = [NSString string];
    for (int j=0; j<9; j++) {
        if (sendStr.length > 0) {
            sendStr = [NSString stringWithFormat:@"%@,%d",sendStr,cmdData[j]];
        } else {
            sendStr = [NSString stringWithFormat:@"%d",cmdData[j]];
        }
    }
    sendStr = [NSString stringWithFormat:@"%@,%d,%d,%d,%d",sendStr,redData,greenata,blueData,checkData];
    
    [self sendWifiMsg:sendStr];
    
}

//发送循环渐变
- (void)sendInsetCmd:(insetMode)modeCmd
{
    unsigned char cmdData[] = {0xAA,0x0b,0x01,0x00,0x00,0x01,0x01,0x10,0x03};
    unsigned char checkData  = 0;
    
    for (int i = 0; i<9; i++) {
        checkData += cmdData[i];
    }
    checkData += modeCmd;
    
    NSString *sendStr = [NSString string];
    for (int j=0; j<9; j++) {
        if (sendStr.length > 0) {
            sendStr = [NSString stringWithFormat:@"%@,%d",sendStr,cmdData[j]];
        } else {
            sendStr = [NSString stringWithFormat:@"%d",cmdData[j]];
        }
    }
    sendStr = [NSString stringWithFormat:@"%@,%d,%d",sendStr,modeCmd,checkData];
    
    [self sendWifiMsg:sendStr];
}

- (void)sendPowerCmd:(PowerControlCmd)cmd
{
    unsigned char cmdData[] = {0xAA,0x0b,0x01,0x00,0x00,0x01,0x01,0x10,0x01};
    unsigned char checkData  = 0;
    
    for (int i = 0; i<9; i++) {
        checkData += cmdData[i];
    }
    checkData += cmd;
    
    NSString *sendStr = [NSString string];
    for (int j=0; j<9; j++) {
        if (sendStr.length > 0) {
            sendStr = [NSString stringWithFormat:@"%@,%d",sendStr,cmdData[j]];
        } else {
            sendStr = [NSString stringWithFormat:@"%d",cmdData[j]];
        }
    }
    sendStr = [NSString stringWithFormat:@"%@,%d,%d",sendStr,cmd,checkData];
    
    [self sendWifiMsg:sendStr];
}

#pragma 接收数据
- (BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
{
    
    NSString *string =  [[NSString alloc] initWithData:data  encoding:NSUTF8StringEncoding];
    DLog(@"host----->%@,%@",host,string);
    //这里可以加入你想要的代码
    return YES;
}
-(void)onUdpSocket:(AsyncUdpSocket *)sock didNotReceiveDataWithTag:(long)tag dueToError:(NSError *)error{
    
    NSLog(@"Message not received for error: %@", error);
}

-(void)onUdpSocket:(AsyncUdpSocket *)sock didNotSendDataWithTag:(long)tag dueToError:(NSError *)error{
    
    NSLog(@"Message not send for error: %@",error);
}

-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag{
    
    NSLog(@"Message send success!");
}

-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
    
    NSLog(@"socket closed!");
}




@end
